﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using HyperCrunch.Druid;
using HyperCrunch.Druid.RuleMatchers;
using System;
using System.Text;

namespace HyperCrunch.Druid.ContentModifiers
{
    public class PageDialogContentModifier : IContentModifier
    {
        public void RenderContent(UIContext uiContext, Page page)
        {
            RuleSettings settings = uiContext.JsonSettings;
            foreach (string key in settings.Keys)
            {
                if (key.ToLower() == "pagedialog")
                {
                    Dictionary<string, object> attribs = (Dictionary<string, object>)settings[key];
                    if (attribs.ContainsKey("Url"))
                    {
                        LiteralControl dialog = new LiteralControl();
                        StringBuilder html = new StringBuilder();
                        string dialogWidth = attribs.ContainsKey("Width") ? attribs["Width"].ToString() : "900";
                        string dialogHeight = attribs.ContainsKey("Height") ? attribs["Height"].ToString() : "500";
                        string uniqueId = "X" + DateTime.Now.Ticks.ToString();
                        string closeUrl = (uiContext.Context.Request.ApplicationPath + "/images/close-icn.png").Replace("//", "/");
                        html.Append("<div id=\"" + uniqueId + "\">");
                        html.Append("<img src=\"" + closeUrl + "\" alt=\"Close\">");
                        html.Append("<iframe src=\"" + attribs["Url"] + "\"></iframe>");
                        html.Append("</div>");
                        html.Append("<style>");
                        html.Append("#" + uniqueId + " { background-color: #eee;position: absolute; width:" + dialogWidth + "px; height:" + dialogHeight + "px; z-Index:99999;");
                        html.Append("border:1px solid #ededed;-moz-box-shadow: 10px 10px 5px #aaa; -webkit-box-shadow: 10px 10px 5px #aaa;box-shadow: 10px 10px 5px #aaa;");
                        html.Append("-moz-border-radius: 15px;border-radius: 15px;-webkit-border-radius: 15px;}");
                        html.Append("#" + uniqueId + " iframe { border:1px solid #bbb;margin:10px; width:" + (Convert.ToInt32(dialogWidth) - 22).ToString() + "px; height:" + (Convert.ToInt32(dialogHeight) - 29 - 22).ToString() + "px; }");
                        html.Append("#" + uniqueId + " img { float:right; margin: 5px 5px 0px 5px;border:0; cursor:pointer; }");
                        html.Append("</style>");
                        html.Append("<script type=\"text/javascript\">");
                        html.Append("$(document).ready(function() {");
                        html.Append(" $('#" + uniqueId + "').css('top', (($(window).height() - $('#" + uniqueId + "').outerHeight()) / 2) + $(window).scrollTop() + 'px')");
                        html.Append("                       .css('left', (($(window).width() - $('#" + uniqueId + "').outerWidth()) / 2) + $(window).scrollLeft() + 'px');");
                        html.Append(" $('#" + uniqueId + " img').click(function() {");
                        html.Append("     $('#" + uniqueId + "').html('').hide()");
                        html.Append(" });");
                        html.Append("});");
                        html.Append("</script>");
                        dialog.Text = html.ToString();
                        Control body = page.FindControl("body");
                        body.Controls.Add(dialog);
                        break;
                    }
                }
            }
        }
    }
}
